<?php

namespace App\Providers;

use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Config;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any application authentication / authorization services.
     *
     * @param  \Illuminate\Contracts\Auth\Access\Gate  $gate
     * @return void
     */
    public function boot(GateContract $gate)
    {
        parent::registerPolicies($gate);

        $acls = config('acl');

        foreach($acls as $acl=>$sons){
            $gate->define($acl, function ($user,$obj='') use ($acl) {
                //dd($user);
                return $user->hasPermission($acl);
            });

            foreach($sons as $key=>$son){
                if($key!='name'){
                    //echo "<BR>key:".$key;
                    $gate->define($key, function ($user,$obj='') use ($key) {
                        //dd($user);
                        return $user->hasPermission($key);
                    });
                }//end if()
            }//end foreach
        }
        
        //dd($gate);
        //dd($this);
        //
    }
}
